package com.james.codeauto.util;

import com.james.codeauto.common.StringUtils;
import com.james.codeauto.tools.json.JSONArray;
import com.james.codeauto.tools.json.JSONObject;
import org.apache.poi.ooxml.POIXMLDocument;
import org.apache.poi.ooxml.extractor.POIXMLTextExtractor;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.IBodyElement;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;

/**
 * @Author：hp
 * @Description：
 * @Date：2021年11月4日14:58:11
 * @Modified by：读取word所有内容
 **/
public class ParkingLotJieShunDocUtil {
    public static void main(String[] args) {
        try {
            parseHtmlData();
        } catch (Exception e) {
            e.printStackTrace();
        }
        // String content = readWord(parseDocFromGreenCloudPath());
        // System.out.println(content);
    }

    public static List<Map<String, Object>> parseHtmlData() throws Exception {
        HashSet<String> paramKeyTypes = new HashSet<>();
        List<Map<String, Object>> listData = new ArrayList<>();
        FileInputStream fileOutputStream = new FileInputStream(parseHtmlFromGreenCloudPath());
        String htmlText = new String(fileOutputStream.readAllBytes());
        fileOutputStream.close();
        Document doc = Jsoup.parse(htmlText);
        Elements h2AllElement = doc.getElementsByTag("h2");
        Elements h2Element = new Elements();
        int firstIndex = -1, endIndex = -1;
        for (int i = 0; i <= h2AllElement.size() - 1; i++) {
            String text = h2AllElement.get(i).text();
            if (text.contains("5.2.1 获取签名密钥(key)")) {
                firstIndex = i;
            }
            if (firstIndex < 0) {
                continue;
            }
            if (endIndex > 0) {
                continue;
            }
            if (text.contains("5.3 设备管理")) {
                continue;
            }
            if (text.contains("5.4 组织结构")) {
                continue;
            }
            if (text.contains("5.5 人事资料")) {
                continue;
            }
            if (text.contains("5.6 车场业务接口")) {
                continue;
            }
            if (text.contains("5.7 门禁业务接口")) {
                continue;
            }
            if (text.contains("6.1 车场业务接口")) {
                continue;
            }
            if (text.contains("6.2 门禁业务接口")) {
                continue;
            }
            if (text.contains("7.1 停车场业务")) {
                endIndex = 1;
                continue;
            }

            h2Element.add(h2AllElement.get(i));


        }

        System.out.println(h2Element.size());
        Elements childE = doc.getElementsByClass("MsoNormalTable");
        Elements pNewAllElement = new Elements();
        int firstFind = -1, endP = -1;
        for (int i = 0; i < childE.size(); i++) {
            if (endP > 0) {
                continue;
            }
            Elements tr = childE.get(i).getElementsByTag("tbody").first().getElementsByTag("tr");
            String text = tr.get(0).getElementsByTag("td").get(0).text();

            if (!StringUtils.isNotBlank(text.trim()) || text.contains("接口名称：") || text.contains("接口地址：") || text.contains("接口描述：") || text.contains("返回参数：")) {
                int begin = 0;
                if (!StringUtils.isNotBlank(text.trim())) {
                    begin = 1;
                }
                for (int j = begin; j < tr.size(); j++) {
                    Element td = tr.get(j).getElementsByTag("td").get(1);
                    if (td.text().equals("工行")) {
                        endP = 1;
                        break;
                    }
                    if (td.text().equals("访客类型")) {
                        continue;
                    }
                    if (firstFind < 0) {
                        firstFind = 1;
                        pNewAllElement.add(new Element("td").text("获取签名密钥"));
                        pNewAllElement.add(new Element("td").text("/base/getKey"));
                        pNewAllElement.add(new Element("td").text("HTTP POST"));
                        pNewAllElement.add(new Element("td").text("获取签名密钥"));
                        pNewAllElement.add(new Element("td").text("详见“请求参数说明”"));
                        pNewAllElement.add(new Element("td").text("详见“返回参数说明”"));

                    }
                    pNewAllElement.add(td);

                }
            }


        }


        //按照path 分组，不够进行补齐
        //6*49 = 294
        Elements pNewElement = pNewAllElement;
        int flag = -1;
        int countFlag = 0;
        Elements tbody0Element = new Elements();
        int ignoreEnd = -1;
        int totalFind = 0;
        for (int i = 0; i < childE.size(); i++) {
            if (ignoreEnd > 0) {
                continue;
            }

            Elements tr = childE.get(i).getElementsByTag("tbody").first().getElementsByTag("tr");
            String text = tr.get(0).getElementsByTag("td").get(0).text();
            if (!text.trim().equals("参数")) {
                //all in one.
                continue;
            }

            //requet 合并

            //response 合并
            Element table = childE.get(i).getElementsByTag("tbody").first();
            if (tr.size() == 1) {
                //next maybe combine check
                tr = childE.get(i + 1).getElementsByTag("tbody").first().getElementsByTag("tr");
                text = tr.get(0).getElementsByTag("td").get(0).text();

                if (text.equals("code")) {
                    Element table2 = childE.get(i + 1).getElementsByTag("tbody").first();

                    for (int j = i + 2; j < i + 5; j++) {
                        Elements trk = childE.get(j).getElementsByTag("tbody").first().getElementsByTag("tr");
                        String textk = trk.get(0).getElementsByTag("td").get(0).text();
                        if (textk.equals("错误代码（code）")) {
                            i = j - 1;
                            break;
                        } else {
                            Elements trk2 = table2.getElementsByTag("tr");
                            for (int k = 0; k < trk.size(); k++) {
                                if (trk.get(k).getElementsByTag("td").get(0).text().trim().equals("参数")) {
                                    continue;
                                }
                                trk2.add(trk.get(k));
                            }

                        }
                    }
                    //   System.out.println("@@" + childE.get(i).text() + "\n");
                    tbody0Element.add(table2);

                } else {
                    //找最近的 参数表 进行合并。
                    Element table2 = childE.get(i + 1).getElementsByTag("tbody").first();

                    for (int j = i + 2; j < i + 5; j++) {
                        Elements trk = childE.get(j).getElementsByTag("tbody").first().getElementsByTag("tr");
                        String textk = trk.get(0).getElementsByTag("td").get(0).text();
                        if (textk.equals("参数")) {
                            i = j - 1;
                            break;
                        } else {
                            Elements trk2 = table2.getElementsByTag("tr");
                            for (int k = 0; k < trk.size(); k++) {
                                if (trk.get(k).getElementsByTag("td").get(0).text().trim().equals("参数")) {
                                    continue;
                                }
                                trk2.add(trk.get(k));
                            }

                        }
                    }
                    tbody0Element.add(table2);
                    //request combine check.
                }
                continue;
            }
            String isCodeStr = tr.get(1).getElementsByTag("td").get(0).text();
            if (isCodeStr.equals("code") || isCodeStr.contains("HTTP 状态码为 200 时")) {
                if (isCodeStr.contains("HTTP 状态码为 200 时")) {
                    tr.remove(0);
                }
                //错误代码（code）
                for (int j = i + 1; j < i + 5; j++) {
                    Elements trk = childE.get(j).getElementsByTag("tbody").first().getElementsByTag("tr");
                    String textk = trk.get(0).getElementsByTag("td").get(0).text();
                    if (textk.equals("错误代码（code）")) {
                        i = j - 1;
                        break;
                    } else {
                        Elements trk2 = table.getElementsByTag("tr");
                        for (int k = 0; k < trk.size(); k++) {
                            if (trk.get(k).getElementsByTag("td").get(0).text().trim().equals("参数")) {
                                continue;
                            }

                            trk2.add(trk.get(k));
                        }

                    }
                }
                //      System.out.println("@@" + childE.get(i).text() + "\n");
            } else {
//                for (int j = i + 1; j < i + 10; j++) {
//                    if(j>=266){
//                        continue;
//                    }
//                    Elements trk = childE.get(j).getElementsByTag("tbody").first().getElementsByTag("tr");
//                    String textk = trk.get(0).getElementsByTag("td").get(0).text();
//                    if (textk.equals("参数")) {
//                        i=j;
//                        break;
//                    } else {
//                        Elements trk2 = table.getElementsByTag("tr");
//                        for (int k = 0; k < trk.size(); k++) {
//                            if(trk.get(k).getElementsByTag("td").get(0).text().trim().equals("参数")){
//                                continue;
//                            }
//
//                            trk2.add(trk.get(k));
//                        }
//
//                    }
//                }
            }


            countFlag++;
            tbody0Element.add(table);

        }
        //108/2=49
        tbody0Element.remove(tbody0Element.size() - 1);
        tbody0Element.remove(0);
        int code = 0;
//        for (int i = 0; i < tbody0Element.size(); i++) {
//            System.out.println(tbody0Element.get(i).text());
//            if(tbody0Element.get(i).text().contains("code")&&!tbody0Element.get(i).text().contains("qrcode")){
//                code++;
//                System.out.println("\n");
//            }
//        }
//        System.out.println(code+"\n");
        List<Element> subList = new ArrayList<>();
        List<List<Element>> subArrayList = new ArrayList<>();
        for (int i = 0; i < tbody0Element.size(); i++) {
            Element element = tbody0Element.get(i);
            subList.add(element);
            if (element.text().contains("code") && !element.text().contains("qrcode")) {

                subArrayList.add(subList);
                subList = new ArrayList<>();
            }

        }
        Elements elementsTable = new Elements();
        for (int i = 0; i < subArrayList.size(); i++) {
            List<Element> subItemList = subArrayList.get(i);
            Element element = subItemList.get(0);
            Elements er = element.getElementsByTag("tr");
            for (int j = 1; j < subItemList.size() - 1; j++) {
                for (int k = 0; k < subItemList.get(j).getElementsByTag("tr").size(); k++) {
                    er.add(subItemList.get(j).getElementsByTag("tr").get(k));
                }
            }
            elementsTable.add(element);
            elementsTable.add(subItemList.get(subItemList.size() - 1));
        }

        System.out.println("==" + elementsTable.size());
        //49 * 2 = 98
        System.out.println(tbody0Element.size());
        Elements tbodyElement = elementsTable;
//        for (int i = 0; i < tbodyElement.size(); i++) {
//            System.out.println(tbodyElement.get(i).text());
//            if(tbodyElement.get(i).text().contains("code")&&!tbodyElement.get(i).text().contains("qrcode")){
//                System.out.println("\n");
//            }
//        }

        int tbodyElementSize = tbodyElement.size() / 2;
        System.out.println(tbodyElementSize + "");//2个一组

        for (int i = 0; i < h2Element.size(); i++) {
            Map<String, Object> parseMap = new HashMap<>();
            String title = h2Element.get(i).text();
            parseMap.put("title", title.split(" ")[1].trim());
            String titleT = (String) parseMap.get("title");
            parseMap.put("title", titleT);

            //method ->  测试环境： https://acscloudtest.parking24.cn/open_platform/portal/outer/get_park_info
            int descIndex = i * 6 + 3, urlIndex = i * 6 + 1;
//
            if (descIndex >= pNewElement.size()) {
                continue;
            }
            parseMap.put("desc", pNewElement.get(descIndex).text().replace("简要描述：", "").trim());
            parseMap.put("url", pNewElement.get(urlIndex).text().replace("Path：", "").replace("测试环境： ", "").trim().replace("测试环境: ", "").trim().replace("http://192.168.xx.xx:port", ""));
            parseMap.put("method", pNewElement.get(i * 6 + 2).text().replace("HTTP", "").trim());
            //parseMap.put("methodType", parseMap.get("method").toString().toLowerCase());

            Elements table1Element = tbodyElement.get(2 * i).getElementsByTag("tr");
            JSONArray paramInfo = new JSONArray();
            if (table1Element != null) {

                for (int j = 0; j < table1Element.size(); j++) {
                    Elements table1TdElement = table1Element.get(j).getElementsByTag("td");
                    JSONObject jsonObjectParam = new JSONObject();
                    for (int k = 0; k < table1TdElement.size(); k++) {
                        Element td = table1TdElement.get(k);
                        String paramStr = td.text();
                        if (k == 0) {
                            //参数名
                            // String style = td.getElementsByTag("span").first().attr("style");
                            jsonObjectParam.put("treeNode", "0");
//                            if (style.equals("padding-left: 0px")) {
//
//                            } else if (style.equals("padding-left: 20px")) {
//                                jsonObjectParam.put("treeNode", "1");
//                            } else if (style.equals("padding-left: 40px")) {
//                                jsonObjectParam.put("treeNode", "2");
//                            } else if (style.equals("padding-left: 60px")) {
//                                jsonObjectParam.put("treeNode", "3");
//                            } else if (style.equals("padding-left: 80px")) {
//                                jsonObjectParam.put("treeNode", "4");
//                            }
                            jsonObjectParam.put("name", paramStr.replace("├─", "").replaceAll(" ", "").trim());
                            jsonObjectParam.put("realName", paramStr.trim());
                        } else if (k == 4) {
                            //必选
                            if (paramStr.trim().equals("是")) {
                                jsonObjectParam.put("require", "是");
                            } else {
                                jsonObjectParam.put("require", "否");
                            }


                        } else if (k == 2) {

                            paramKeyTypes.add(paramStr);
                            //默认值
                            jsonObjectParam.put("type", paramStr);
                        } else if (k == 3) {
                            //说明
                            jsonObjectParam.put("defaultValue", "" + paramStr);
                            jsonObjectParam.put("descMore", "" + paramStr);
                        } else if (k == 1) {
                            //说明
                            jsonObjectParam.put("desc", paramStr);
                        } else if (k == 5) {
                            //说明
                            jsonObjectParam.put("descMore", "" + paramStr);
                        }

                        System.out.println(paramStr);
                    }
                    if (jsonObjectParam.get("name").toString().contains("{")) {
                        parseMap.put("reqOrgJsonStr", jsonObjectParam.get("name").toString().substring(jsonObjectParam.get("name").toString().indexOf("{")));
                    }

                    if (jsonObjectParam.get("name").toString().contains("参数")) {
                        continue;
                    }

                    if (jsonObjectParam.get("name").toString().trim().contains("Json 示例")) {
                        continue;
                    }
                    if (!StringUtils.isNotBlank(jsonObjectParam.optString("type"))) {
                        continue;
                    }
                    if (!StringUtils.isNotBlank(jsonObjectParam.optString("require"))) {
                        continue;
                    }
                    System.out.println(jsonObjectParam.toString());
                    String reuqire = (String) jsonObjectParam.get("require");
                    String type = (String) jsonObjectParam.get("type");
                    if ("是".equals(reuqire) || "否".equals(reuqire)) {

                    } else {
                        jsonObjectParam.put("type", reuqire);
                        jsonObjectParam.put("require", type);
                    }
                    paramInfo.put(jsonObjectParam);
                }
            }
            parseMap.put("paramList", paramInfo);
            Elements table2Element = tbodyElement.get(2 * i + 1).getElementsByTag("tr");

            JSONArray responseParamInfo = new JSONArray();
            if (table2Element != null) {
                for (int j = 0; j < table2Element.size(); j++) {
                    Elements table2TdElement = table2Element.get(j).getElementsByTag("td");

                    JSONObject jsonObjectParam = new JSONObject();
                    int len = table2TdElement.size();
                    for (int k = 0; k < len; k++) {
                        Element td = table2TdElement.get(k);

                        String responseParamStr = td.text();
                        System.out.println(responseParamStr);
                        if (k == 0) {
                            //参数名
                            //String style = td.getElementsByTag("span").first().attr("style");
                            jsonObjectParam.put("treeNode", "0");
//                            if (style.equals("padding-left: 0px")) {
//
//                            } else if (style.equals("padding-left: 20px")) {
//                                jsonObjectParam.put("treeNode", "1");
//                            } else if (style.equals("padding-left: 40px")) {
//                                jsonObjectParam.put("treeNode", "2");
//                            } else if (style.equals("padding-left: 60px")) {
//                                jsonObjectParam.put("treeNode", "3");
//                            } else if (style.equals("padding-left: 80px")) {
//                                jsonObjectParam.put("treeNode", "4");
//                            }
                            jsonObjectParam.put("name", responseParamStr.replace("├─", "").replaceAll(" ", "").trim());
                            jsonObjectParam.put("realName", responseParamStr.trim());
                        } else if (k == 4) {
                            //必选
                            if (responseParamStr.trim().equals("是")) {
                                jsonObjectParam.put("require", "是");
                            } else {
                                jsonObjectParam.put("require", "否");
                            }

                        } else if (k == 2) {

                            paramKeyTypes.add(responseParamStr);
                            //默认值
                            jsonObjectParam.put("type", responseParamStr.trim());
                        } else if (k == 3 || k == 5) {
                            //说明
                            jsonObjectParam.put("defaultValue", "" + responseParamStr);
                            jsonObjectParam.put("descMore", "" + responseParamStr);
                        } else if (k == 1) {
                            //说明
                            jsonObjectParam.put("desc", responseParamStr);
                        } else if (k == 5) {
                            //说明
                            jsonObjectParam.put("descMore", "" + responseParamStr);
                        }
                        System.out.println(responseParamStr);
                    }
                    if (jsonObjectParam.get("name").toString().contains("{")) {
                        parseMap.put("respOrgJsonStr", jsonObjectParam.get("name").toString().substring(jsonObjectParam.get("name").toString().indexOf("{")));
                    }
                    if (jsonObjectParam.get("name").toString().contains("参数")) {
                        continue;
                    }
                    if (!StringUtils.isNotBlank(jsonObjectParam.get("name").toString().trim())) {
                        continue;
                    }
                    if (jsonObjectParam.get("name").toString().trim().contains("Json 示例")) {
                        continue;
                    }
                    if (!StringUtils.isNotBlank(jsonObjectParam.optString("type"))) {
                        continue;
                    }
                    if (!StringUtils.isNotBlank(jsonObjectParam.optString("require"))) {
                        continue;
                    }
                    jsonObjectParam.put("treeNode", "0");
                    String reuqire = (String) jsonObjectParam.get("require");
                    String type = (String) jsonObjectParam.get("type");
                    if ("是".equals(reuqire) || "否".equals(reuqire)) {

                    } else {
                        jsonObjectParam.put("type", reuqire);
                        jsonObjectParam.put("require", type);
                    }
                    responseParamInfo.put(jsonObjectParam);

                }
            }
            parseMap.put("responseParamList", responseParamInfo);

            //构造json
            JSONObject jsonParamObject = convertListParamToJsonObj(true, paramInfo, parseMap);
            parseMap.put("paramJson", jsonParamObject);
            parseMap.put("paramJsonStr", jsonParamObject.toString(4));

            JSONObject responseJsonParamObject = convertListParamToJsonObj(false, responseParamInfo, parseMap);
            parseMap.put("responseJson", responseJsonParamObject);
            parseMap.put("responseParamJsonStr", responseJsonParamObject.toString(4));

            JSONObject response = (JSONObject) parseMap.get("responseJson");
            if (response.opt("data") != null) {
                if (StringUtils.isNotBlank(response.optString("pageSize"))) {
                    parseMap.put("hasPage", "true");
                }
                if (response.get("data") instanceof JSONArray) {
                    parseMap.put("responseJson", response.optJSONArray("data"));
                } else {
                    parseMap.put("responseJson", response.optJSONObject("data"));
                }
            }


            JSONObject paramJson = (JSONObject) parseMap.get("paramJson");
            if (paramJson.opt("data") != null) {
                if (paramJson.get("data") instanceof JSONArray) {
                    parseMap.put("paramJson", paramJson.optJSONArray("data"));
                } else {
                    parseMap.put("paramJson", paramJson.optJSONObject("data"));
                }
            }


            String reqJsonCommentStr = "";
            for (int j = 0; j < paramInfo.length(); j++) {
                JSONObject reqParamObject = paramInfo.getJSONObject(j);
                String desc = reqParamObject.optString("desc");
                if (!StringUtils.isNotBlank(desc)) {
                    desc = reqParamObject.getString("name");
                }
                String type = reqParamObject.optString("type");
                if (!StringUtils.isNotBlank(type)) {
                    type = "Object";
                }
                reqJsonCommentStr += reqParamObject.getString("name") + "\t";
                reqJsonCommentStr += desc + "\t";
                reqJsonCommentStr += type + "\t";
                reqJsonCommentStr += reqParamObject.optString("require") + "\t";
                reqJsonCommentStr += "" + "\t";
                reqJsonCommentStr += desc + "\n";

            }
            parseMap.put("reqJsonCommentStr", reqJsonCommentStr);

            String jsonCommentStr = "";
            for (int j = 0; j < responseParamInfo.length(); j++) {
                JSONObject responseParamObject = responseParamInfo.getJSONObject(j);
                String desc = responseParamObject.optString("desc");
                if (!StringUtils.isNotBlank(desc)) {
                    desc = responseParamObject.getString("name");
                }
                String type = responseParamObject.optString("type");
                if (!StringUtils.isNotBlank(type)) {
                    type = "Object";
                }
                jsonCommentStr += responseParamObject.getString("name") + "\t";
                jsonCommentStr += desc + "\t";
                jsonCommentStr += type + "\t";
                jsonCommentStr += responseParamObject.optString("require") + "\t";
                jsonCommentStr += "" + "\t";
                jsonCommentStr += desc + "\n";

            }
            parseMap.put("jsonCommentStr", jsonCommentStr);

            checkSaveMarkdownToFile(parseMap);


            listData.add(parseMap);
        }

        System.out.println("paramKeyTypes===" + paramKeyTypes.toString());
        return listData;
    }

    private static int currentIndex = 0;

    private static boolean hasReqtJsonStr(String info) {
        if (info.substring(0, info.indexOf("{")).contains("响应参数")) {
            return false;
        }
        return true;
    }

    //number, boolean, string, integer, object, object [], string [],
    public static JSONObject convertListParamToJsonObj(boolean request, JSONArray paramInfo, Map<String, Object> parseMap) {
//        if (true) {
//            return convertListParamToJsonObj(paramInfo);
//        }

        try {
            String info = "";
            System.out.println("" + parseMap.get("url"));
            if (request) {
                info = (String) parseMap.get("reqOrgJsonStr");
                if (parseMap.get("url").toString().contains("/park/downparkcredential")) {
                    info = "{\"seqId\":\"3248f7gd-bb68-418a-a8ad-fd56e84547b8\",\"parkServices\":[{\"itemId\":\"0408e7ef-bb68-418a-a8ad-fd56e84369b9\",\"deviceApList\":[{\"deviceApGuid\":\"07b3e8a6-fa9e-4955-90d2-d4e6502ec843\"},{\"deviceApGuid\":\"3ac00095-50fc-4f4b-9e12-57affc66ac9a\"}],\"credential\":[{\"cardId\":\"08b3e8a6-ea9e-6955-90d2-d4e6502ec911\",\"credentialType\":163,\"credentialNo\":\"粤 B12345\",\"carId\":\"96f3e8a6-ea9e-6955-90d2-d4e6502ec822\",\"vehicleInfo\":{\"plateColor\":\"BLUE\"}}],\"parkServiceOperateType\":\"OPEN\",\"setmealNo\":50,\"personId\":\"606e1fab-e3b8-48b0-b175-654db3d84d7c\",\"serviceStartDate\":\"2019-07-17\",\"serviceEndDate\":\"2019-08-16\",\"parkServiceGuid\":\"506e1fab-e3b8-48b0-b175-654db3d84d7b\",\"placeNum\":1}]}";
                } else if (parseMap.get("url").toString().contains("/door/downdoorcredential")) {
                    info = "{\"cardServices\":[{\"itemId\":\"6cdc7b83-719e-41e3-9e12-c39de934093a\",\"deviceList\":[{\"deviceGuid\":\"6a883f19-8443-4bf8-a278-d1097a570a0f\",\"isDeleted\":0}],\"isDeleted\":0,\"cardFaceNo\":\"22323456\",\"credentialType\":\"1\",\"credentialNo\":\"22323456\",\"credentialStatus\":\"NORMAL\",\"personId\":\"6cdc7b83-719e-41e3-9e12-c39de934090b\",\"serviceStartDate\":\"2019-07-22\",\"serviceEndDate\":\"2019-08-22\",\"startTime1\":\"00:00\",\"endTime1\":\"23:59\",\"week\":\"1111111\",\"cardKey\":\"1234\",\"templateData\":\"\"}]}";
                }
            } else {
                info = (String) parseMap.get("respOrgJsonStr");
                if (parseMap.get("url").toString().contains("/base/depts")) {
                    info = "{\"data\":{\"depts\":[{\"deptId\":\"3bf45554-be38-4858-a714-c41277104a89\",\"deptNo\":99961520,\"deptName\":\"test1\",\"parentId\":\"f041b1ac-5170-499d-81b3-7dfef5e9ea64\",\"remark\":null},{\"deptId\":\"f041b1ac-5170-499d-81b3-7dfef5e9ea64\",\"deptNo\":11497898,\"deptName\":\"test\",\"parentId\":\"00000000-0000-0000-0000-000000000000\",\"remark\":\"\"}],\"pageIndex\":1,\"pageSize\":2,\"pageCount\":1,\"totalCount\":2},\"code\":\"0\",\"msg\":\"\"}";
                } else if (parseMap.get("url").toString().contains("/park/inparkingrecord")) {
                    info = "{\"code\":\"0\",\"msg\":\"\",\"data\":{\"totalCount\":200,\"pageIndex\":1,\"pageSize\":10,\"pageCount\":20,\"records\":[{\"inRecordId\":\"26e caf80470d490786a4f7e3eca832b3\",\"parkId\":\"34ebff70583f476986a4f7e3ffa742c5\",\"inDeviceId\":\"98762087123e483fa96f176d7eaefe52\",\"inDeviceName\":\"入口\",\"inTime\":\"2018 - 03 - 07 10: 01: 00\",\"plateNumber\":\"粤 - B12345 \",\"plateColor\":\"blue\",\"inImage\":\"http: //jielink.jieshun.com/api/base/image?id=1234.jpg\",\"stationOperator\":\"admin\",\"sealName\":\"临时用户 A\",\"parkingTime\":\"2 天 17 时 17 分 39 秒\"}]}}";
                } else if (parseMap.get("url").toString().contains("/park2/inparkingrecord")) {
                    info = "";
                } else if (parseMap.get("url").toString().contains("/park/downparkcredential")) {
                    info = "{\"data\":{\"resultList\":[{\"itemId\":\"0408e7ef-bb68-418a-a8ad-fd56e84369b9\",\"result\":0,\"message\":\"\"}]},\"code\":\"0\",\"msg\":\"\"}";
                } else if (parseMap.get("url").toString().contains("/park/bookcarlist")) {
                    info = "{\"code\":\"0\",\"msg\":\"\",\"data\":{\"bookNum\":\"202101150001\",\"plateNumber\":\"粤 A12345\",\"bookTime\":\"2021-01-15 10:01:00\",\"overdueTime \":\"2021-01-15 23:59:59\",\"bookPlace\":\"0002\"}}";
                } else if (parseMap.get("url").toString().contains("/base/getqrcodedata")) {
                    info = "{\"data\":{\"guid\":\"6cdc7b83-719e-41e3-9e12-c39de934093a9\",\"qrCodeData\":\"\"},\"code\":\"0\",\"msg\":\"\"}";
                }else if (parseMap.get("url").toString().contains("/outrecognition")) {
                    info = "{\"code\":\"0\",\"msg\":\"\"}";
                }else if (parseMap.get("url").toString().contains("/carin")) {
                    info = "{\"code\":\"0\",\"msg\":\"\"}";
                }else if (parseMap.get("url").toString().contains("/door/thirdauthopendoor")) {
                    info = "{\"code\":\"0\",\"msg\":\"\"}";
                }else if (parseMap.get("url").toString().contains("/paycheck")) {
                    info = "{\"data\":{\"payType\":\"ZFB\",\" transactionId\":\"123456\",\"chargeTime\":\"2018 - 03 - 22 15: 00: 12 \",\"payStatus\":0},\"code\":\"0\",\"msg\":\"\"}";
                }else if (parseMap.get("url").toString().contains("/park2/inparkingrecord")) {
                    info = "";
                }
            }
            if (StringUtils.isNotBlank(info)) {
                JSONObject jsonObject = new JSONObject(info.substring(info.indexOf("{")));
                return jsonObject;

            }

            return convertListParamToJsonObj(paramInfo);


        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static JSONObject convertListParamToJsonObj(JSONArray paramInfo) {
        JSONArray a = new JSONArray();
        for (int i = 0; i < paramInfo.length(); i++) {
            a.put(paramInfo.getJSONObject(i));
        }
        boolean hasMsg = false;
        for (int i = a.length() - 1; i >= 0; i--) {
            if (a.getJSONObject(i).getString("name").equals("code")) {
                a.remove(i);
                hasMsg = true;
                continue;
            }
            if (a.getJSONObject(i).getString("name").equals("msg")) {
                a.remove(i);
            }
        }
        JSONObject jsonObject = new JSONObject();
        currentIndex = 0;
        for (currentIndex = 0; currentIndex < a.length(); currentIndex++) {
            JSONObject paramObj = a.getJSONObject(currentIndex);
            parseParam(a, paramObj, jsonObject, 0);
        }
        JSONObject jsondataObject = new JSONObject();
        if (hasMsg) {

            jsondataObject.put("data", jsonObject);
            jsondataObject.put("msg", "ok");
            jsondataObject.put("code", "0");
        } else {
            jsondataObject = jsonObject;
        }
        return jsondataObject;
    }

    private static JSONObject parseParam(JSONArray paramInfo, JSONObject paramObj, JSONObject jsonObject, int currentNode) {
        String name = paramObj.optString("name").trim();
        if (StringUtils.isNotBlank(name)) {
            String type = paramObj.optString("type");
            if (type.equals("number")) {
                jsonObject.put(name, 1.0);
            } else if (type.equals("integer")) {
                jsonObject.put(name, 1);
            } else if (type.equals("boolean")) {
                jsonObject.put(name, true);
            } else if (type.equals("string []") || type.equals("number []")) {
                JSONArray childObjArray = new JSONArray();
                childObjArray.put("String");
                jsonObject.put(name, childObjArray);
            } else if (type.equals("object")) {
                JSONObject childObj = new JSONObject();
                //treeNodeCheck
                currentIndex++;
                childObj = parseParam(paramInfo, paramInfo.getJSONObject(currentIndex), childObj, Integer.parseInt(paramObj.getString("treeNode")) + 1);
                jsonObject.put(name, childObj);
            } else if (type.equals("object []")) {
                JSONArray childObjArray = new JSONArray();
                //treeNodeCheck
                currentIndex++;
                for (int i = 0; i < 1; i++) {
                    JSONObject childObj = new JSONObject();
                    childObj = parseParam(paramInfo, paramInfo.getJSONObject(currentIndex), childObj, Integer.parseInt(paramObj.getString("treeNode")) + 1);
                    childObjArray.put(childObj);
                }
                jsonObject.put(name, childObjArray);
            } else if (type.equals("string")) {
                String defaultValue = paramObj.optString("defaultValue");
                if (StringUtils.isNotBlank(defaultValue)) {
                    jsonObject.put(name, defaultValue);
                } else {
                    jsonObject.put(name, "String");
                }

            }

            if (currentNode == Integer.parseInt(paramObj.getString("treeNode"))) {
                //in sub node
                if (paramInfo.length() > currentIndex + 1) {
                    JSONObject paramCheck = paramInfo.getJSONObject(currentIndex + 1);
                    if (currentNode == Integer.parseInt(paramCheck.getString("treeNode"))) {
                        //still the same
                        currentIndex++;
                        jsonObject = parseParam(paramInfo, paramCheck, jsonObject, Integer.parseInt(paramObj.getString("treeNode")));
                    }


                }

            }
        }
        return jsonObject;
    }

    static boolean saveMarkDownFile = false;

    private static void checkSaveMarkdownToFile(Map<String, Object> parseMap) throws Exception {
        if (saveMarkDownFile) {
            String title = (String) parseMap.get("title");
            String fileName = title.replaceAll("/", "-") + ".md";
            // 从JDK1.7开始提供的方法
            // 使用Files.write创建一个文件并写入
            try {
                Files.write(Paths.get(fileName), GenMarkDownUtils.generatorCode(parseMap).getBytes(StandardCharsets.UTF_8));
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

    public static String parseHtmlFromGreenCloudPath() {
        String html = "捷顺停车智能终端操作平台API接口说明V1.2.html";
        String path = Paths.get("doc", html).toAbsolutePath().toString();
        return path;
    }

    public static String parseDocFromGreenCloudPath() {
        String html = "绿云会员接口手册.docx";
        String path = Paths.get("doc", html).toAbsolutePath().toString();
        return path;
    }

    public static String processParagraphs(XWPFParagraph paragraph, XWPFDocument doc) {

        List<XWPFRun> runs = paragraph.getRuns();
        String paragraphText = "";
        for (XWPFRun run : runs) {
            String text = run.getText(0);                                                                       //分段是poi来完成的,但是它分段的时候可能出现${CJ_CP}${CL_LX}这种情况
            System.out.println("text---->" + text);
            if (text != null) {
                paragraphText += text;
            }
        }
        System.out.println("@@@@=" + paragraphText);
        return paragraphText;
    }

    public static String readWord(String path) {
        String buffer = "";
        try {
            if (path.endsWith(".docx")) {
                InputStream is2 = new FileInputStream(new File(path));
                XWPFDocument docx = new XWPFDocument(is2);
                for (int i = 0; i < docx.getBodyElements().size(); i++) {
                    IBodyElement element = docx.getBodyElements().get(i);
                    if (element instanceof XWPFParagraph) {
                        XWPFParagraph xwpfParagraph = (XWPFParagraph) element;
                        processParagraphs(xwpfParagraph, docx);
                    } else {
                        //table.

                    }
                }
                int tables = docx.getTables().size();
                System.out.println(tables);
                docx.close();

                OPCPackage opcPackage = POIXMLDocument.openPackage(path);
                POIXMLTextExtractor extractor = new XWPFWordExtractor(opcPackage);
                buffer = extractor.getText();
                extractor.close();
            }
//            else if (path.endsWith("docx")) {
//                OPCPackage opcPackage = POIXMLDocument.openPackage(path);
//                POIXMLTextExtractor extractor = new XWPFWordExtractor(opcPackage);
//                buffer = extractor.getText();
//                extractor.close();
//            }
            else {
                System.out.println("此文件不是word文件！");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return buffer;
    }
}
